#include "includes.h"

#define IDC_SIZE_MM    100
#define IDC_SIZE_INCH  110

#define MY_ES_DIGIT_ONLY 0x0001
#define MY_ES_ALPHA_ONLY 0x0002

static WNDPROC old_edit_proc;

extern HWND Window1;

HWND Window2;

/**
 * 对话框一共含有4个控件，分别用于
 * 1.显示提示信息、2.用户输入框、
 * 3.显示转换后的长度、4.关闭程序用的“确定”按钮
 */
static CTRLDATA CtrlInputLen[] = 
{
    {
        CTRL_STATIC,
        WS_VISIBLE | SS_SIMPLE,
        10, 10, 380, 18,
        IDC_STATIC,
        "PleaseEnterTheLength(Unit:Mm)",
        0
    },
    {
        CTRL_EDIT,
        WS_VISIBLE | WS_TABSTOP | WS_BORDER,
        10, 40, 380, 24,
        IDC_SIZE_MM,
        NULL,
        0
    },
    {
        CTRL_STATIC,
        WS_VISIBLE | SS_SIMPLE,
        10, 70, 380, 18,
        IDC_SIZE_INCH,
        "EquivalentTo 0.00 Inch",
        0
    },
    {
        CTRL_BUTTON,
        WS_TABSTOP | WS_VISIBLE | BS_DEFPUSHBUTTON,
        170, 100, 60, 25,
        IDOK,
        "Determine",
        0
    }
};

/* 定义对话框模板 */
static DLGTEMPLATE DlgBoxInputLen = {
    WS_BORDER | WS_CAPTION,
    WS_EX_NONE,
    120, 150, 400, 160,
    "PleaseEnterALength",
    0, 0,
    sizeof(CtrlInputLen) / sizeof(CTRLDATA), NULL,
    0
};

/**
 * @函数功能：自定义控件只可以输入数组或者字母
 */ 
// typedef LRESULT (* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
static int RestrictedEditBox(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == MSG_CHAR)
    {
        DWORD my_style = GetWindowAdditionalData(hWnd);
        /* 确定被屏蔽的按键类型 */
        if ((my_style & MY_ES_DIGIT_ONLY) && (wParam < '0' || wParam > '9'))
        {
            return 0; /* 被屏蔽的按键消息直接返回 */
        }
        else if ((my_style & MY_ES_ALPHA_ONLY) && (!(((wParam >= 'A') && (wParam <= 'Z')) || (wParam >= 'a') && (wParam <= 'Z'))))
        {
            return 0;
        }
    }
    /* 由老窗口过程处理其余消息 */
    return (*old_edit_proc)(hWnd, message, wParam, lParam);
}

/**
 * @函数功能：输入框的通知回调函数
 */
static void my_notif_proc(HWND hWnd, LINT id, int nc, DWORD add_data)
{
    if (id == IDC_SIZE_MM && nc == EN_CHANGE) {
        char buf[60];
        double len;

        GetWindowText(hWnd, buf, 32);
        len = atof(buf);
        len = len / 25.4;
        sprintf(buf, "EquivalentTo %.5f Inch", len);
        SetDlgItemText(GetParent(hWnd), IDC_SIZE_INCH, buf);
    }
}

/**
 * @函数功能：对话框信息处理函数
 */ 
static LRESULT InputLenDialogBoxProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
    case MSG_INITDIALOG:
        /* 将通过DialogBoxIndirectParam的最后一个参数传递的数据指针保存下来*/
        SetWindowAdditionalData(hDlg, lParam);
        SendDlgItemMessage(hDlg, IDC_SIZE_MM, EM_LIMITTEXT, 15, 0); /* 设置编辑控件文本限制 */
        /* 设置控件通知回调函数 */
        SetNotificationCallback(GetDlgItem(hDlg, IDC_SIZE_MM), my_notif_proc);
        return 1;
    
    case MSG_COMMAND:
        switch (wParam)
        {
        case IDOK:
            {
                char buf[40];
                double *length = (double *)GetWindowAdditionalData(hDlg);
                GetWindowText(GetDlgItem(hDlg, IDC_SIZE_MM), buf, 32);
                *length = atof(buf);
            }
        case IDCANCEL:
            EndDialog(hDlg, wParam);
            break;
        default:
            break;
        }
        break;
    
    case MSG_CLOSE:
        EndDialog(hDlg, IDCANCEL);
        break;
    
    default:
        break;
    }
    return DefaultDialogProc(hDlg, message, wParam, lParam);
}

static int InputLenDialogBox(HWND hWnd, double* length)
{
    DlgBoxInputLen.controls = CtrlInputLen;
    return DialogBoxIndirectParam(&DlgBoxInputLen, hWnd, InputLenDialogBoxProc, (LPARAM)length);
}

static LRESULT MiniGUIProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    double length;
    HWND hWnd1;
    
    static int create_num = 0;
    switch (message) {
    case MSG_CREATE:
        CreateWindow(CTRL_BUTTON, "New Input Dialog", 
        WS_CHILD | BS_PUSHBUTTON | BS_CHECKED | WS_VISIBLE,
        IDC_WIN2_BUTTON_ONE, 75, 150, 150, 50, hWnd, 0); /* 创建控件-按钮 */
        
        hWnd1 = CreateWindow(CTRL_EDIT, "", WS_CHILD | WS_BORDER | WS_VISIBLE, IDC_WIN2_CTRL_ONE,
        75, 100, 150, 50, hWnd, MY_ES_ALPHA_ONLY); /* 创建一个只可以输入字母的编辑框 */

        old_edit_proc = SetWindowCallbackProc(hWnd1, RestrictedEditBox);

        break;

    case MSG_COMMAND:
        switch (wParam) {
        case IDC_WIN2_BUTTON_ONE:
        {
            int ret = InputLenDialogBox(hWnd, &length);   /* 新建对话框 */
            if (ret == IDOK) {
                printf("the length is %.5f mm\n", length);
            }
            return 0;
        }
        
        default:
            break;
        }
        break;

    case MSG_CLOSE:
        DestroyMainWindow(hWnd); /* 销毁窗口、但不退出 */
        return 0;
    default:
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
 
void WindowSecond(HWND hWnd)
{
    MSG Msg;
    MAINWINCREATE CreateInfo;
    

    #ifdef _MGRM_PROCESSES
    JoinLayer(NAME_DEF_LAYER, "MiniGUI", 0, 0);
    #endif

    CreateInfo.dwStyle = WS_VISIBLE | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_THINFRAME | WS_CAPTION;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "Window-II";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MiniGUIProc;
    CreateInfo.lx = 50;
    CreateInfo.ty = 100;
    CreateInfo.rx = 300;
    CreateInfo.by = 400;
    CreateInfo.iBkColor = COLOR_yellow;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = HWND_DESKTOP;

    Window2 = CreateMainWindow(&CreateInfo);

    ShowWindow(Window2, SW_SHOWNORMAL);
}

#ifdef _MGRM_THREADS
#include <minigui/dti.c>
#endif
